package snippet;


public class T8 {
	public static void main(String[] args) {
		
		//System.out.println('9'*1-48);
		System.out.println(myAtoi("-6133ddd"));
	}
	 public static int myAtoi(String str) {	
			char[] cs=str.trim().toCharArray();
			long count=0l;
			boolean f=false;
			for (int i = 0; i < cs.length; i++) {
				char a=cs[i];
				if(i==0) {
					
					if(a=='-') {
						f=true;
						continue;
					}
					else if(a=='+') {
						continue;
					}
				}
				int t=a*1-48;
				if(t<0||t>9) {
					return (int)count;
				}

				count=count*10+(f?-t:t);
				if(f) {
					if(count<Integer.MIN_VALUE) {
						return Integer.MIN_VALUE;
					}
				}else {
					if(count>Integer.MAX_VALUE) {
						return Integer.MAX_VALUE;
					}
				}
			}
			return (int)count;
	    }
	
}
